package edu.pku.sei.act.classGenerator;

import edu.pku.sei.act.taskStruct.*;
import java.util.Collection;
import java.util.Iterator;

 public class RoleClassGenerator {  
  protected static String nl;
  public static synchronized RoleClassGenerator create(String lineSeparator)
  {
    nl = lineSeparator;
    RoleClassGenerator result = new RoleClassGenerator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "  ";
  protected final String TEXT_2 = NL + NL + "package edu.pku.sei.act.";
  protected final String TEXT_3 = ".";
  protected final String TEXT_4 = ";" + NL + "" + NL + "import ";
  protected final String TEXT_5 = ".actools.util.AbilityOutput;" + NL + "import ";
  protected final String TEXT_6 = ".actools.util.Capability;" + NL + "import ";
  protected final String TEXT_7 = ".actools.util.Role;" + NL + "import ";
  protected final String TEXT_8 = ".actools.util.TaskMessage;" + NL + "import ";
  protected final String TEXT_9 = ".";
  protected final String TEXT_10 = ".";
  protected final String TEXT_11 = ";" + NL + "" + NL + "public class ";
  protected final String TEXT_12 = "{" + NL + "\t" + NL + "\tpublic static final String ROLE_NAME = \"";
  protected final String TEXT_13 = "\";" + NL + "\tpublic static final String REWARDS = \"\";" + NL + "" + NL + "\tpublic static class state {" + NL + "\t" + NL + "\t\t";
  protected final String TEXT_14 = NL + "\t\t\tpublic static final String ";
  protected final String TEXT_15 = " = \"";
  protected final String TEXT_16 = "\";" + NL + "\t\t";
  protected final String TEXT_17 = "\t\t" + NL + "\t}" + NL + "" + NL + "\tpublic static class transformation {" + NL + "\t" + NL + "\t\t";
  protected final String TEXT_18 = NL + "\t\t\tpublic static final String ";
  protected final String TEXT_19 = " = \"";
  protected final String TEXT_20 = "\";" + NL + "\t\t";
  protected final String TEXT_21 = "\t\t" + NL + "\t}" + NL + "" + NL + "\tpublic static class capability {" + NL + "\t" + NL + "\t\t";
  protected final String TEXT_22 = NL + "\t\t\tpublic static AbilityOutput ";
  protected final String TEXT_23 = "_OUTPUT = new AbilityOutput();" + NL + "\t\t\tpublic static final Capability ";
  protected final String TEXT_24 = " = new Capability(null, \"";
  protected final String TEXT_25 = "\", null, Capability.DAEMON_CAPABILITY, null, \"";
  protected final String TEXT_26 = "\");" + NL + "\t\t";
  protected final String TEXT_27 = "\t\t\t" + NL + "\t}" + NL + "" + NL + "\tpublic static class communication {" + NL + "\t\t";
  protected final String TEXT_28 = NL + "\t\t \tpublic static final TaskMessage ";
  protected final String TEXT_29 = " = new TaskMessage(";
  protected final String TEXT_30 = ".getTask().getTaskName(),null,null,\"";
  protected final String TEXT_31 = "\", null);" + NL + "\t\t \t" + NL + "\t\t ";
  protected final String TEXT_32 = "\t\t" + NL + "\t}" + NL + "}";
  protected final String TEXT_33 = NL;

 public String generate(String taskName, Role role)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
     String pre_package = "edu.pku.sei.act"; 
    stringBuffer.append(TEXT_2);
    stringBuffer.append( NameGenerator.genTaskPackageName(taskName) );
    stringBuffer.append(TEXT_3);
    stringBuffer.append( NameGenerator.genRolePackageName(role.getName()));
    stringBuffer.append(TEXT_4);
    stringBuffer.append(pre_package);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(pre_package);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(pre_package);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(pre_package);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(pre_package);
    stringBuffer.append(TEXT_9);
    stringBuffer.append( NameGenerator.genTaskPackageName(taskName) );
    stringBuffer.append(TEXT_10);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_11);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_12);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_13);
    
		 Collection<State> states = role.getStates();
		 for(Iterator<State> it = states.iterator(); it.hasNext(); )
		 {
		 	State state = it.next(); 
		 
    stringBuffer.append(TEXT_14);
    stringBuffer.append( NameGenerator.genStateName(state.getName()));
    stringBuffer.append(TEXT_15);
    stringBuffer.append( state.getName().toLowerCase());
    stringBuffer.append(TEXT_16);
    		 	
		 }
		
    stringBuffer.append(TEXT_17);
    
		 Collection<Transition> transes = role.getTranses();
		 String pretext = "edu.pku.sei.act." + role.getName().toLowerCase();
		 for(Iterator<Transition> it = transes.iterator(); it.hasNext(); )
		 {
		 	Transition trans = it.next();
		
    stringBuffer.append(TEXT_18);
    stringBuffer.append( NameGenerator.genTransitionName(trans.getName()));
    stringBuffer.append(TEXT_19);
    stringBuffer.append( pretext + "." + trans.getName().toLowerCase());
    stringBuffer.append(TEXT_20);
    
		} 
    stringBuffer.append(TEXT_21);
    
		 Collection<Capability> caps = role.getCapabilities();		
		 for(Iterator<Capability> it = caps.iterator(); it.hasNext(); )
		 {
		 	Capability cap = it.next();
		
    stringBuffer.append(TEXT_22);
    stringBuffer.append( NameGenerator.genCapabilityName(cap.getName()));
    stringBuffer.append(TEXT_23);
    stringBuffer.append( NameGenerator.genCapabilityName(cap.getName()));
    stringBuffer.append(TEXT_24);
    stringBuffer.append( cap.getName().toLowerCase());
    stringBuffer.append(TEXT_25);
    stringBuffer.append( cap.getName().toLowerCase());
    stringBuffer.append(TEXT_26);
    
		} 
    stringBuffer.append(TEXT_27);
     
		 Collection<TaskMessage> msgs = role.getTaskMessages();
		 for(Iterator<TaskMessage> it = msgs.iterator(); it.hasNext(); )
		 {
		 	TaskMessage msg = it.next();
		 
    stringBuffer.append(TEXT_28);
    stringBuffer.append(NameGenerator.genTaskMessageName(msg.getName()));
    stringBuffer.append(TEXT_29);
    stringBuffer.append( NameGenerator.genTaskProcessorName(taskName));
    stringBuffer.append(TEXT_30);
    stringBuffer.append(msg.getTopic());
    stringBuffer.append(TEXT_31);
    
		 } 
    stringBuffer.append(TEXT_32);
    stringBuffer.append(TEXT_33);
    return stringBuffer.toString();
  }
}